/**
 *  Copyright 2002 Quaseem
 *  Copyright 2004 Migisan
 *  Copyright 2008 Oest
 *
 *  This file is part of the aochat package.
 *
 *  The aochat package is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The aochat package is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with the aochat package; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package sk.sigp.aobot.client.core;

import java.sql.Date;

import sk.sigp.aobot.client.BotCore;
import sk.sigp.aobot.client.types.Text;
import sk.sigp.aobot.client.types.UserId;

public class User {
	public BotCore boss;

	public UserId user_id = new UserId(-1);
	public String nick = "loll";

	public String firstname = "";
	public String lastname = "";
	public int level = 0;
	public String breed = "";
	public String gender = "";
	public String faction = "";
	public String profession = "";
	public String profession_title = "";
	public int organization_id = 0;
	public String organization = "";
	public int rank_id = 0;
	public String rank = "";

	protected Date detail_date = new Date(0);

	public String alias = "";

	protected Date last_login = new Date(0);
	protected Date last_logout = new Date(0);

	public boolean isBot = false;
	public boolean isOwner = false;
	public boolean isBuddy = false;
	public boolean isOrgMember = false;
	public boolean isInChatGroup = false;
	public boolean isOnline = false;

	public User(BotCore b) {
		boss = b;
	}

	public User(BotCore b, String name, UserId id) {
		boss = b;
		user_id = id;
		nick = name;
		readDetails();
	}

	public User(BotCore b, Text name, UserId id) {
		this(b, name.getStringData(), id);
	}

	public User(BotCore b, String name, int id) {
		this(b, name, new UserId(id));
	}

	public User(BotCore b, UserId id) {
		this(b, "", id);
	}

	public static String nameConvention(String name) {
		if (name.length() > 1)
			return name.substring(0, 1).toUpperCase()
					+ name.substring(1).toLowerCase();
		else
			return name;
	}

	public void setAlias(String str) {
		alias = str;
	}

	public void setBot(boolean b) {
		isBot = b;
	}

	public void setOwner(boolean b) {
		if (isOwner != b) {
			isOwner = b;
		}
	}

	public void setBuddy(boolean b) {
		if (isBuddy != b) {
			isBuddy = b;
		}
	}

	public void setOrgMember(boolean b) {
		if (isOrgMember != b) {
			isOrgMember = b;
		}
	}

	public void setInChatGroup(boolean b) {
		if (isInChatGroup != b) {
			isInChatGroup = b;
		}
	}

	public void setOnline(boolean b) {
		if (isOnline && !b) {
			last_logout = new Date(System.currentTimeMillis());
		}
		if (!isOnline && b) {
			last_login = new Date(System.currentTimeMillis());
		}
		if (isOnline != b) {
			isOnline = b;
		}
	}

	/**
	 * get characterdata from the web database, XML format
	 */
	public void readDetails() {
//		if (detail_date.getTime() < System.currentTimeMillis() - 48 * 60 * 60
//				* 1000) {
//			detail_date = new Date(System.currentTimeMillis());
//			String name = nick.toLowerCase();
//			URL thisUserURL = null;
//			try {
//				thisUserURL = new URL(
//						"http://www.anarchy-online.com/character/bio/d/"
//								+ dimension + "/name/" + name + "/bio.xml");
//				if (1 == 1)
//					return;
//				BufferedReader input = new BufferedReader(
//						new InputStreamReader(thisUserURL.openStream()));
//				String content = "";
//				java.lang.Thread.sleep(500);
//				while (input.ready())
//					content += input.readLine();
//
//				this.firstname = findString(content, "firstname", "");
//				this.lastname = findString(content, "lastname", "");
//				this.level = findInt(content, "level", 0);
//				this.breed = findString(content, "breed", "");
//				this.gender = findString(content, "gender", "");
//				this.faction = findString(content, "faction", "");
//				this.profession = findString(content, "profession", "");
//				this.profession_title = findString(content, "profession_title",
//						"");
//				this.organization_id = findInt(content, "organization_id", 0);
//				this.organization = findString(content, "organization_name", "");
//				this.rank_id = findInt(content, "rank_id", 0);
//				this.rank = findString(content, "rank", "");
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
	}

//	private String findString(String s, String tag, String alt) {
//		int a = s.indexOf("<" + tag + ">");
//		int e = s.indexOf("</" + tag + ">");
//		a += tag.length() + 2;
//		if (a < e)
//			return s.substring(a, e);
//		else
//			return alt;
//	}
//
//	private int findInt(String s, String tag, int alt) {
//		int a = s.indexOf("<" + tag + ">");
//		int e = s.indexOf("</" + tag + ">");
//		a += tag.length() + 2;
//		if (a < e) {
//			Integer i = new Integer(s.substring(a, e));
//			return i.intValue();
//		} else
//			return alt;
//	}
}
